#!/bin/bash

# 定义下载函数
download_qubic() {
    echo "请选择更新的apoolminer版本:"
    echo "更新完锄头请重新配置文件"
    echo "1. apoolminer"
    echo "2. 跳过更新"
    read rqiner_choice

    case $rqiner_choice in
        1)
            echo "正在下载apoolminer..."
            wget -O apoolminer http://106.54.62.152/apoolminer && chmod 777 apoolminer
            if [ $? -ne 0 ]; then
                echo "下载失败，请检查网络连接或链接是否正确。"
            fi
            ;;
        2)
            echo "跳过 apoolminer 更新"
            ;;
        *)
            echo "无效选择"
            ;;
    esac
}

download_xmrig() {
    echo "是否更新 xmrig?"
    echo "1. 更新"
    echo "2. 跳过更新"
    read xmrig_choice

    case $xmrig_choice in
        1)
            echo "正在下载xmrig..."
            wget -O xmrig https://gitee.com/dudu1luchang/dudu/raw/master/xmrig && sudo chmod 777 xmrig
            if [ $? -ne 0 ]; then
                echo "下载失败，请检查网络连接或链接是否正确。"
            fi
            ;;
        2)
            echo "跳过 xmrig 更新"
            ;;
        *)
            echo "无效选择"
            ;;
    esac
}

# 主菜单循环
while true; do
    # 显示菜单
    echo "apool + zeph，请选择一个操作:"
    echo "第一次使用请先更新锄头，再配置文档之后启动挖矿..."
    echo "有问题请联系：du.叫兽（微信号duduyiluchang）"
    echo " "
    echo "1. 更新锄头"
    echo "2. 配置文档"
    echo "3. 查看配置"
    echo "4. 启动挖矿"
    echo "5. 停止挖矿"
    echo "6. 查看日志"
    echo "0. 退出脚本"

    # 获取用户选择
    read choice

    case $choice in
        1)
            # 更新 apoolminer 和 xmrig
            download_qubic
            download_xmrig
            ;;
        2)
            echo "配置文档..."
            
            # 获取 apool的账号
            echo "请输入apool的账号:"
            read account
            account=${account:-CP_a8l9cekfgf}

            # 获取 apool 的线程数
            echo "apool的线程数 [默认: 0]:"
            read apool_threads
            apool_threads=${apool_threads:-0}

            # 获取矿机名称（默认值：dudu）
            echo "请输入矿机名称 [默认: dudu]:"
            read miner_name
            miner_name=${miner_name:-dudu}

            # 获取 apool 池子地址（默认值：qubic1.hk.apool.io:3334）
            echo "apool池子地址（默认为qubic1.hk.apool.io:3334）:"
            read apool_pool
            apool_pool=${apool_pool:-qubic1.hk.apool.io:3334}
 
            # 获取 zeph 池子地址（默认值：www.duduyiluchang.store:1123）
            echo "zeph池子地址（默认为www.duduyiluchang.store:1123）:"
            read zeph_pool
            zeph_pool=${zeph_pool:-www.duduyiluchang.store:1123}
            
            # 获取 zeph 钱包地址
            echo "请输入zeph钱包地址:"
            read zeph_address
            zeph_address=${zeph_address:-ZEPHs95zw4Thmw3kDm1opBa5sWFCb5GgRG4UPmYcAsx5NC4mFsq3RBLDUj6X6tzDncZqcJyoSEeQvjSDe75D85dhV1SNyijsdmH}

            # 获取 zeph 线程数（默认值：0）
            echo "请输入zeph线程数 [默认: 0]:"
            read threads
            threads=${threads:-0}

            # 创建 apool_zeph.conf 文件并写入配置
            echo "account=$account" > apool_zeph.conf
            echo "apool_threads=$apool_threads" >> apool_zeph.conf
            echo "threads=$threads" >> apool_zeph.conf
            echo "miner_name=$miner_name" >> apool_zeph.conf
            echo "zeph_address=$zeph_address" >> apool_zeph.conf
            echo "zeph_pool=$zeph_pool" >> apool_zeph.conf
            echo "apool_pool=$apool_pool" >> apool_zeph.conf

# 读取 apool_zeph.conf 中的配置
if [[ -f "apool_zeph.conf" ]]; then
    while IFS='=' read -r key value; do
        # 去除空格
        key=$(echo "$key" | xargs)
        value=$(echo "$value" | xargs)
        # 将变量导出为环境变量
        export "$key"="$value"
    done < "apool_zeph.conf"
else
    echo "apool_zeph.conf 文件不存在！"
    exit 1
fi


# 下载 miner.conf 文件
wget -O miner.conf https://gitee.com/dudu1luchang/dudu/raw/master/miner.conf && sudo chmod 777 miner.conf

# 使用 sed 替换 miner.conf 文件中的内容
sudo sed -i "s/AAAA/$account/g" miner.conf
sudo sed -i "s/CCCC/$apool_threads/g" miner.conf
sudo sed -i "s/HHHH/$threads/g" miner.conf
sudo sed -i "s/BBBB/$miner_name/g" miner.conf
sudo sed -i "s/FFFF/$zeph_address/g" miner.conf
sudo sed -i "s/EEEE/$zeph_pool/g" miner.conf
sudo sed -i "s/DDDD/$apool_pool/g" miner.conf



            # 提示配置完成
            echo "配置文档已经完成，请稍等..."
            
            # 下载新的脚本文件
            wget -O run.sh https://gitee.com/dudu1luchang/dudu/raw/master/run.sh && chmod 777 run.sh
            ;;
        3)
            echo "查看配置..."
            # 在这里添加查看配置的代码
            if [ -f "apool_zeph.conf" ]; then
                cat apool_zeph.conf
            else
                echo "配置文件 apool_zeph.conf 不存在，请先运行配置文档。"
            fi
            ;;
        4)
          echo "启动挖矿..."
          # 在后台运行并且不记录日志
        nohup bash run.sh > run.log 2>&1 &
            echo "脚本已在后台运行。"
            ;;

        5)
            echo "停止挖矿..."
            # 在这里添加停止挖矿的代码
            pkill -f "run.sh"
            pkill -f "apoolminer"
            pkill -f "xmrig"
            echo "挖矿已停止。"
            ;;
        6)
            # 检查是否存在至少一个日志文件
            if [ -f "qubic.log" ] || [ -f "xmrig.log" ]; then
                echo "正在查看日志，按 q 退出查看并返回菜单..."
                sleep 1

                # 如果 qubic.log 存在，则查看 qubic.log
                if [ -f "qubic.log" ]; then
                    tail -f qubic.log &   # 通过后台进程查看 qubic.log
                    tail_pid_qubic=$!     # 获取后台进程的PID
                fi

                # 如果 xmrig.log 存在，则查看 xmrig.log
                if [ -f "xmrig.log" ]; then
                    tail -f xmrig.log &   # 通过后台进程查看 xmrig.log
                    tail_pid_xmrig=$!     # 获取后台进程的PID
                fi

                while true; do
                    # 监听用户输入，1秒钟检查一次
                    read -t 1 -n 1 input
                    if [[ "$input" == "q" ]]; then
                        echo "退出日志查看，返回菜单..."
                        # 结束 tail -f 进程
                        if [ -n "$tail_pid_qubic" ]; then
                            kill $tail_pid_qubic   # 结束 tail -f qubic.log 进程
                        fi
                        if [ -n "$tail_pid_xmrig" ]; then
                            kill $tail_pid_xmrig   # 结束 tail -f xmrig.log 进程
                        fi
                        break
                    fi
                done
            else
                echo "日志文件 qubic.log 或 xmrig.log 不存在，请先启动挖矿。"
            fi
            ;;
        0)
            echo "退出脚本"
            exit 0
            ;;
        *)
            echo "无效选择"
            ;;
    esac
done
